﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using BitPortal.Business;
using System.Collections.ObjectModel;
using System.Collections;



namespace BitPortal.Admin.Service.Work
{
    public partial class Default : Controls.BaseAdminPage
    {
        protected void Page_OnInit(object sender, EventArgs e)
        {
            PermissionBLL.CheckRoleThrowException(role: "WorkView", useExceptionHandler: true);
            base.OnInit(e);
            (this.workAddEdit1 as Controls.BaseAdminControl).ReloadControls.Add(this.GridPanel1);
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        private void LoadGridData()
        {
            (this.Master as AdminFunction).MasterResourceManager.AddScript("Ext.getCmp('" + GridPanel1.ClientID + "').getStore().reload();");
            (this.GridPanel1.SelectionModel.Primary as RowSelectionModel).ClearSelections();
        }
        protected void btnAdd_Click(object sender, DirectEventArgs e)
        {
            try
            {
               // PermissionBLL.CheckRoleThrowException("WorkInsert", useExceptionHandler: true);
                workAddEdit1.BindData(0);
                LoadGridData();
            }
            catch (System.Exception ex)
            {
                this.ShowNotification(ex.Message, "Thông báo ", Icon.Add);
            }
        }

        protected void btnEdit_Click(object sender, DirectEventArgs e)
        {
            try
            {
                PermissionBLL.CheckRoleThrowException("WorkEdit", useExceptionHandler: true);
                RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
                if (sm.SelectedRows.Count > 0)
                {
                    long ID = Convert.ToInt64(sm.SelectedRows[0].RecordID);
                    workAddEdit1.BindData(ID);
                }
                
            }
            catch (SystemException ex)
            {
                this.ShowNotification(ex.Message, "Thông báo lỗi ", Icon.Error);
            }
        }

        protected void btnDelete_Click(object sender, DirectEventArgs e)
        {

            try
            {
                //PermissionBLL.CheckRoleThrowException("WorkDelete",useExceptionHandler:true);
                ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
                RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    long ID = Convert.ToInt64(row.RecordID);
                    BitPortal.Models.WorkWarranty work = bitEnt.WorkWarranties.SingleOrDefault(ent => ent.WorkWarrantyID == ID);
                    if (work != null)
                    {
                        work.IsDeleted =true ;
                    }
                }
                int record = bitEnt.SaveChanges();
                Notification.Show(new NotificationConfig
                {
                    Title = "Thông báo",
                    Icon = Icon.Information,
                    Html = "Xóa " + record + " công việc thành công!"
                });
                sm.ClearSelections();
                LoadGridData();
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message);
            }
        }

        protected void btnTrashEmpty_Click(object sender, DirectEventArgs e)
        {
            try
            {
                PermissionBLL.CheckRoleThrowException("WorkDelete");
                int record = 0;
                ExtendedBitPortalEntities bitext = new ExtendedBitPortalEntities();
                foreach (BitPortal.Models.WorkWarranty a in bitext.WorkWarranties.Where(ent => ent.IsDeleted == true).ToList())
                {
                    if (ArticleBLL.DeletePermanently(a.WorkWarrantyID) > 0)
                    {
                        record++;
                    }
                }
                Notification.Show(new NotificationConfig
                {
                    Title = "Thông báo",
                    Icon = Icon.Information,
                    Html = "Xóa " + record + " công việc trong thùng rác thành công!"
                });
                LoadGridData();
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message);
            }
        }
        protected void btnTrashDelete_Click(object sender, DirectEventArgs e)
        {
            try
            {
                PermissionBLL.CheckRoleThrowException("WorkDelete",useExceptionHandler:true);

                int record = 0;
                RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    if (WorkWarrantyBLL.DeletePermanently(Convert.ToInt64(row.RecordID)) > 0)
                    {
                        record++;
                    }
                }
                Notification.Show(new NotificationConfig
                {
                    Title = "Thông báo",
                    Icon = Icon.Information,
                    Html = "Xóa " + record + " công việc trong thùng rác thành công!"
                });
                sm.ClearSelections();
                LoadGridData();
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message);
            }
        }

        protected void btnUnDelete_Click(object sender, DirectEventArgs e)
        {
            try
            {
                PermissionBLL.CheckRoleThrowException("WorkEdit",useExceptionHandler:true);
                ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
                RowSelectionModel sm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    long ID = Convert.ToInt64(row.RecordID);
                    BitPortal.Models.WorkWarranty work = bitEnt.WorkWarranties.SingleOrDefault(ent => ent.WorkWarrantyID == ID);
                    if (work != null)
                        work.IsDeleted = false;
                }
                int record = bitEnt.SaveChanges();
                Notification.Show(new NotificationConfig
                {
                    Title = "Thông báo",
                    Icon = Icon.Information,
                    Html = "Xóa " + record + " công việc thành công!"
                });
                LoadGridData();
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message);
            }
        }

        protected void btnTrash_Click(object sender, DirectEventArgs e)
        {
            if (Session["WorkGridFlag"] != null && Session["WorkGridFlag"].Equals(false))
            {
                Session["WorkGridFlag"] = true;
            }
            else
            {
                Session["WorkGridFlag"] = false;
            }
            LoadGridData();
        }

        protected void Store1_RefreshData(object sender, StoreRefreshDataEventArgs e)
        {
            List<Models.WorkWarranty> data;
            if (Session["WorkGridFlag"] != null && Session["WorkGridFlag"].Equals(false))
            {
                ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
                data = bitEnt.WorkWarranties.Where(ent => ent.StateWork == (int)StateWorkEnum.Quantrixacnhan_Hoantat).ToList();
            }
            else
            {
                data = WorkWarrantyBLL.GetAll().ToList();
            }
            FilterConditions fc = null;
            string s = e.Parameters[this.GridFilters1.ParamPrefix];
            if (!string.IsNullOrEmpty(s))
            {
                fc = new FilterConditions(s);
            }
            this.Store1.DataSource = ExtNet.GirdData.Filter<Models.WorkWarranty>(data, fc, e);
        }

        protected void btnSetState_Click(object sender, DirectEventArgs e)
        {
            try
            {
                PermissionBLL.CheckRoleThrowException("WorkEdit", useExceptionHandler: true);
                int state = Convert.ToInt32(cbxState.SelectedItem.Value);

                RowSelectionModel rm = this.GridPanel1.SelectionModel.Primary as RowSelectionModel;
                long id = Convert.ToInt64(rm.SelectedRows[0].RecordID);
                ExtendedBitPortalEntities ent = new ExtendedBitPortalEntities();
                Models.WorkWarranty work = ent.WorkWarranties.SingleOrDefault(pp => pp.WorkWarrantyID == id);
                work.StateWork = state;
                if(state==6)
                {
                    Models.ErrorReport error = ent.ErrorReports.SingleOrDefault(pp => pp.ErrorID == work.ErrorID);
                    error.StateError = (int)StateError.Daxulyxong;
                }
                
                if (ent.SaveChanges()>0)
                {
                    Window4.Hide();
                    ShowNotification("Đã cập nhật công việc thành công");
                }

            }
            catch (System.Exception ex)
            {
                ShowNotification(ex.Message);
            }
        }

        [DirectMethod]
        public void LoadNotes(string note,string noteGroupId,string errorID)
        {
            if (note.Equals("Notes"))
            {
                wWorkNote.AutoLoad.Url = "/Admin/Note/?NoteGroupID=" + noteGroupId;
                wWorkNote.LoadContent();
                wWorkNote.Show();
            }
            else
            {
                this.wProductDetail.AutoLoad.Url = "/Admin/Service/ProductCustomer/?ErrorID=" + errorID;
                this.wProductDetail.LoadContent();
                this.wProductDetail.Show();
            }
        }

        
    }
}